from selenium import webdriver
from .login import login_test
from time import sleep
from ..lib.screenshot_function import screenshot
from selenium.webdriver.chrome.options import Options


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')
    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 报表管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]').click()
        sleep(1)
        # 报表一览
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]/ul').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            if dicts["report_name"] and dicts["task_name"] and dicts["run_mode"] and dicts["scan_type"] and dicts["start_date"] is not None:
                # 筛选条件-报表名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').send_keys(
                    dicts["report_name"])
                sleep(1)
                # 任务名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[2]/div/div/div/input').send_keys(
                    dicts["task_name"])
                sleep(1)
                # 执行方式-立即执行
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[3]/div/div/div/div/input').click()
                sleep(1)
                if dicts["run_mode"] == "手动执行":
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["run_mode"] == "定时执行":
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                elif dicts["run_mode"] == "立即执行":
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[3]').click()
                    sleep(1)
                # 扫描类型-离线扫描
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[1]/div/div/div/div/input').click()
                sleep(1)
                if dicts["scan_type"] == "离线扫描":
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["scan_type"] == "在线扫描":
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                # 开始日期
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').click()
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').send_keys(
                    dicts["start_date"][0])
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[2]').send_keys(
                    dicts["start_date"][1])
                sleep(1)
                # 点击一下
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').click()
                # 查询
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath(
                        '//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
            if dicts["report_name"] is not None:
                # 筛选条件-报表名称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').send_keys(dicts["report_name"])
                sleep(1)
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
            if dicts["task_name"] is not None:
                # 任务名称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[2]/div/div/div/input').send_keys(dicts["task_name"])
                sleep(1)
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath(
                        '//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
            if dicts["run_mode"] is not None:
                # 执行方式-立即执行
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[3]/div/div/div/div[1]/input').click()
                sleep(1)
                if dicts["run_mode"] == "手动执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["run_mode"] == "定时执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                elif dicts["run_mode"] == "立即执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[3]').click()
                    sleep(1)
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath(
                        '//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
            # # 扫描对象-中间件
            # driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[3]/div/div/div/div[1]/span/span').click()
            # sleep(1)
            # driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[1]').click()
            # sleep(1)
            # # 查询
            # driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
            # sleep(1)
            # # 重置
            # driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
            # sleep(1)
            if dicts["scan_type"] is not None:
                # 扫描类型-离线扫描
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[1]/div/div/div/div[1]/input').click()
                sleep(1)
                if dicts["scan_type"] == "离线扫描":
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["scan_type"] == "在线扫描":
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath(
                        '//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
            if dicts["start_date"] is not None:
                # 开始日期
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').click()
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').send_keys(dicts["start_date"][0])
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[2]').send_keys(
                    dicts["start_date"][1])
                sleep(1)
                # 点击一下
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').click()
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(1)
                error_text = driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                print(error_text)
                if error_text == "":
                    error_text = driver.find_element_by_xpath(
                        '//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                # sleep(1)
                sleep(2)
                continue
        print(returns_list)
        return returns_list

    def webui2(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 报表管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]').click()
        sleep(1)
        # 报表一览
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]/ul').click()
        sleep(1)
        # 报表对比
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/header/div[2]/button[1]').click()
        sleep(1)
        # 选中
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr[1]/td[1]/div/label/span').click()
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr[2]/td[1]/div/label/span').click()
        sleep(1)
        # 报表对比
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/header/div[2]/button[1]').click()
        sleep(1)
        # 滚动页面到底部
        js = "var q=document.documentElement.scrollTop=300"
        driver.execute_script(js)
        sleep(2)
        # 滚动页面到顶上
        js = "var q=document.documentElement.scrollTop=0"
        driver.execute_script(js)
        sleep(2)
        # 页面向后退
        driver.back()
        sleep(1)
        # 批量删除
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/header/div[2]/button[2]').click()
        sleep(2)
        # 确定
        driver.find_element_by_xpath('/html/body/div[11]/div/div[3]/button[2]').click()
        sleep(3)
        return "执行成功"

    def webui3(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 报表管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]').click()
        sleep(1)
        # 报表一览
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[5]/ul').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            if dicts["report_name"] and dicts["task_name"] and dicts["run_mode"] and dicts["scan_type"] and dicts["start_date"] is not None:
                # 筛选条件-报表名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').send_keys(
                    dicts["report_name"])
                sleep(1)
                # 任务名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[2]/div/div/div/input').send_keys(
                    dicts["task_name"])
                sleep(1)
                # 执行方式-立即执行
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[3]/div/div/div/div/input').click()
                sleep(1)
                if dicts["run_mode"] == "手动执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["run_mode"] == "定时执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                elif dicts["run_mode"] == "立即执行":
                    driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/ul/li[3]').click()
                    sleep(1)
                # 扫描类型-离线扫描
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[1]/div/div/div/div/input').click()
                sleep(1)
                if dicts["scan_type"] == "离线扫描":
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["scan_type"] == "在线扫描":
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                # 开始日期
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').click()
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').send_keys(
                    dicts["start_date"][0])
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[2]').send_keys(
                    dicts["start_date"][1])
                sleep(1)
                # 点击一下
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').click()
                # 查询
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(2)
                # 删除
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr/td[9]/div/button[2]').click()
                sleep(1)
                # 确定
                driver.find_element_by_xpath('/html/body/div[9]/div/div[3]/button[2]').click()
                # 重置
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                sleep(2)
                # 筛选条件-报表名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').send_keys(
                    dicts["report_name"])
                sleep(1)
                # 任务名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[2]/div/div/div/input').send_keys(
                    dicts["task_name"])
                sleep(1)
                # 执行方式-立即执行
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[3]/div/div/div/div/input').click()
                sleep(1)
                if dicts["run_mode"] == "手动执行":
                    driver.find_element_by_xpath('/html/body/div[8]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["run_mode"] == "定时执行":
                    driver.find_element_by_xpath('/html/body/div[8]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                elif dicts["run_mode"] == "立即执行":
                    driver.find_element_by_xpath('/html/body/div[8]/div[1]/div[1]/ul/li[3]').click()
                    sleep(1)
                # 扫描类型-离线扫描
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[1]/div/div/div/div/input').click()
                sleep(1)
                if dicts["scan_type"] == "离线扫描":
                    driver.find_element_by_xpath('/html/body/div[8]/div[1]/div[1]/ul/li[1]').click()
                    sleep(1)
                elif dicts["scan_type"] == "在线扫描":
                    driver.find_element_by_xpath('/html/body/div[8]/div[1]/div[1]/ul/li[2]').click()
                    sleep(1)
                # 开始日期
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').click()
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[1]').send_keys(
                    dicts["start_date"][0])
                sleep(1)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[2]/div[2]/div/div/div/input[2]').send_keys(
                    dicts["start_date"][1])
                sleep(1)
                # 点击一下
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[1]/div[1]/div/div/div/input').click()
                # 查询
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[1]').click()
                sleep(2)
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                print(error_text)
                if error_text == "暂无数据":
                    returns_list.append("删除成功")
                else:
                    returns_list.append("删除失败")
                # 重置
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div[3]/button[2]').click()
                sleep(2)
                continue
        print(returns_list)
        return returns_list

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()